package com.jzo2o.foundations.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.RegionMapper;
import com.jzo2o.foundations.mapper.ServeItemMapper;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.ServeService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author hu
 * @date 2025/3/13
 */
@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements ServeService {

    @Resource
    private ServeItemMapper serveItemMapper;

    @Resource
    private RegionMapper regionMapper;

    @Resource
    private ServeMapper serveMapper;

    //分页查询区域服务
    @Override
    public PageResult<ServeResDTO> pageSelect(ServePageQueryReqDTO servePageQueryReqDTO) {
        return PageHelperUtils.selectPage(servePageQueryReqDTO,
                () -> baseMapper.queryServeListByRegionId(servePageQueryReqDTO.getRegionId()));
    }

    //新增区域服务
    @Override
    @Transactional
    public void batchAdd(List<ServeUpsertReqDTO> dtos) throws Exception {

        for (ServeUpsertReqDTO dto : dtos) {

            //1.检查服务项是否是启用状态
            ServeItem serveItem = serveItemMapper.selectById(dto.getServeItemId());
            if (ObjectUtils.isEmpty(serveItem) || serveItem.getActiveStatus() != FoundationStatusEnum.ENABLE.getStatus()) {
                throw new Exception("当前服务项目未启用");
            }
            //1.2检查是否重复
            Integer count = lambdaQuery().eq(Serve::getServeItemId, dto.getServeItemId())
                    .eq(Serve::getRegionId, dto.getRegionId()).count();
            if (count > 0) {
                throw new Exception("当前服务项目" + serveItem.getName() + "已存在启用");
            }

            //2.新增服务项目
            Serve serve = BeanUtil.toBean(dto, Serve.class);
            Region region = regionMapper.selectById(dto.getRegionId());
            serve.setCityCode(region.getCityCode());
            baseMapper.insert(serve);
        }


    }

    @Override
    public void updatePriceById(Long id, BigDecimal price) {
        LambdaUpdateWrapper<Serve> wrapper = Wrappers.<Serve>lambdaUpdate().eq(Serve::getId, id).set(Serve::getPrice, price);
        int count = serveMapper.update(null, wrapper);
        if (count < 0) {
            throw new CommonException("修改服务价格失败");
        }

    }

    @Override
    public void ShelvesServe(Long id) {

        Serve serve = serveMapper.selectById(id);
        if (ObjectUtils.isEmpty(serve)) {
            throw new CommonException("区域服务不存在");
        }
        Integer status = serve.getSaleStatus();
        if (!(status == FoundationStatusEnum.DISABLE.getStatus() || status == FoundationStatusEnum.INIT.getStatus())) {
            throw new CommonException("草稿或下架状态才可上架");
        }
        Long serveItemId = serve.getServeItemId();
        ServeItem serveItem = serveItemMapper.selectById(serveItemId);
        if (ObjectUtils.isEmpty(serveItem)) {
            throw new CommonException("服务项不存在");
        }
        Integer activeStatus = serveItem.getActiveStatus();
        //如果服务状态不是上架
        if (activeStatus != FoundationStatusEnum.ENABLE.getStatus()) {
            serveMapper.update(serve, Wrappers.<Serve>lambdaUpdate().eq(Serve::getId, id).set(Serve::getSaleStatus, FoundationStatusEnum.ENABLE.getStatus()));
        }


    }

    //删除区域服务
    @Override
    public void deleteServe(Long id) {
        Serve serve = serveMapper.selectById(id);
        Integer saleStatus = serve.getSaleStatus();
        if (saleStatus != FoundationStatusEnum.INIT.getStatus()) {
            throw new CommonException("区域服务是草稿才可以删除");
        }
        serveMapper.deleteById(serve);
    }

    //下架区域服务
    @Override
    public void offSale(Long id) {
        Serve serve = serveMapper.selectById(id);
        if (ObjectUtils.isEmpty(serve)) {
            throw new CommonException("区域服务不存在，无法下架");
        }
        Integer status = serve.getSaleStatus();
        if (status == FoundationStatusEnum.ENABLE.getStatus()) {
            serve.setSaleStatus(FoundationStatusEnum.DISABLE.getStatus());
            serveMapper.updateById(serve);
        }


    }

    @Override
    public void onHot(Long id) {
        Serve serve = serveMapper.selectById(id);
        if (ObjectUtils.isEmpty(serve)) {
            throw new CommonException("当前区域服务不存在");
        }
        if (serve.getSaleStatus() != FoundationStatusEnum.ENABLE.getStatus()) {
            throw new CommonException("当前区域服务未上架");
        }
        Integer isHot = serve.getIsHot();
        if (isHot == 0) {
            serve.setIsHot(1);
            serveMapper.update(serve, Wrappers.<Serve>lambdaQuery().eq(Serve::getId, id));
        }
    }

    @Override
    public void offHot(Long id) {
        Serve serve = serveMapper.selectById(id);
        if (ObjectUtils.isEmpty(serve)) {
            throw new CommonException("当前区域服务不存在");
        }
        Integer saleStatus = serve.getSaleStatus();
        if (saleStatus == FoundationStatusEnum.DISABLE.getStatus() || saleStatus == FoundationStatusEnum.INIT.getStatus()) {
            throw new CommonException("不能对未上架的区域服务进行热门设置");
        }
        Integer isHot = serve.getIsHot();
        if (isHot == 1) {
            serveMapper.update(serve, Wrappers.<Serve>lambdaUpdate().eq(Serve::getId, id).set(Serve::getIsHot, 0));
        }
    }
}
